import 'package:flutter/cupertino.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';

typedef VoidCallback = void Function();

class RefresherWidget extends StatelessWidget {
  final RefreshController refreshController;
  final bool enablePullDown;
  final bool enablePullUp;
  final Widget? child;
  final VoidCallback? onLoading;
  final VoidCallback? onRefresh;

  const RefresherWidget({
    super.key,
    required this.refreshController,
    required this.enablePullDown,
    required this.enablePullUp,
    this.child,
    this.onLoading,
    this.onRefresh,
  });

  @override
  Widget build(BuildContext context) {
    return SmartRefresher(
      enablePullDown: enablePullDown,
      enablePullUp: enablePullUp,
      onLoading: onLoading,
      onRefresh: onRefresh,
      controller: refreshController,
      // header: const WaterDropHeader(), // 刷新效果
      // 可以自定義
      header: CustomHeader(
        builder: (BuildContext context, RefreshStatus? mode) {
          Widget body = const Text("");
          Widget img = Image.asset('assets/refresh.png');
          if (mode == RefreshStatus.idle) {
            body = const Text("刷新");
          } else if (mode == RefreshStatus.refreshing) {
            body = const CupertinoActivityIndicator();
            img = Image.asset('assets/refresh.gif', width: 60,);
          } else if (mode == RefreshStatus.canRefresh) {
            body = const Text("鬆開刷新");
            img = Image.asset('assets/refresh.gif', width: 60,);
          } else if (mode == RefreshStatus.completed) {
            body = const Text("刷新完成");
            img = Image.asset('assets/refresh.gif', width: 60,);
          } else if (mode == RefreshStatus.failed) {
            body = const Text("刷新失敗");
          } else {
            body = const Text("再試試吧!");
          }
          return SizedBox(
            width: 60.0,
            child: Center(
              child: Column(
                children: [
                  img,
                  body,
                ],
              ),
            ),
          );
        },
      ),
      footer: CustomFooter(
        builder: (BuildContext context, LoadStatus? mode) {
          Widget body;
          if (mode == LoadStatus.idle) {
            body = const Text("下拉加載");
          } else if (mode == LoadStatus.loading) {
            body = const CupertinoActivityIndicator();
          } else if (mode == LoadStatus.failed) {
            body = const Text("加載失敗");
          } else if (mode == LoadStatus.canLoading) {
            body = const Text("取消加載");
          } else {
            body = const Text("---我也是有底綫的---");
          }
          return Container(
            child: Center(
              child: body,
            ),
          );
        },
      ),
      child: child,
    );
  }
}
